/**
 * Copyright (C) 2005-2011 by Insider Guides, LLC                       
 * info@framewerk.org                                                   
 *                                                                      
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the      
 * "Software"), to deal in the Software without restriction, including  
 * without limitation the rights to use, copy, modify, merge, publish,  
 * distribute, sublicense, and#or sell copies of the Software, and to   
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:                                            
 *                                                                      
 * The above copyright notice and this permission notice shall be       
 * included in all copies or substantial portions of the Software.      
 *                                                                      
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,      
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF   
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR    
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.                                      
 */
//Marks the right margin of code *******************************************************************
package com.rmc.utils
{
  
  //--------------------------------------
  //  Imports
  //--------------------------------------
  import flash.display.DisplayObject;
  import flash.display.DisplayObjectContainer;
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.text.TextFormat;
  
  
  //--------------------------------------
  //  Metadata
  //--------------------------------------
  
  //--------------------------------------
  //  Class
  //--------------------------------------
  /**
   * <p>The <code>DisplayObjectHelper</code> is designed to help <code>MovieClip</code> and <code>Sprite</code> operations.</p>
   * 
   * <p>AUTHOR 		: Samuel Asher Rivello</p>
   * <p>COMPANY 		: Rivello Multimedia Consulting</p>
   * <p>CREATION DATE 	: Dec 30, 2010</p>
   * 
   * @example Here is a code example. 
   *  
   * <listing version="3.0">
   * <p>Describe this code example.</p>
   *
   * </listing>
   *
   */
  public class DisplayObjectHelper
  {		
    
    //--------------------------------------
    //  Properties
    //--------------------------------------
    //PUBLIC GETTER/SETTERS
    
    //PUBLIC CONST
    
    //PRIVATE
    
    //--------------------------------------
    //  Constructor
    //--------------------------------------
    
    
    //--------------------------------------
    //  Methods
    //--------------------------------------		
    //PUBLIC	
    
    //PUBLIC STATIC
    /**
     * Determines if a <code>DisplayObject</code> has a certain frame label anywhere in its timeline
     * 
     * @param the_movieClip : *
     * 
     * @param frameLabel_str String to search for
     * 
     * @return Boolean 
     * 
     */		
    public static function hasFrameLabel(the_movieClip : *, frameLabel_str : String) : Boolean 
    {
      var frameLabelFound_str : String;
      var hasFrameLabel_boolean : Boolean = false;
      //
      for(var i:uint=0; i<the_movieClip.currentLabels.length; i++) {
        
        frameLabelFound_str = (the_movieClip.currentLabels[i].name);
        //Debugger.debug("frameLabelFound_str : " + frameLabelFound_str);
        
        if (frameLabelFound_str == frameLabel_str) {
          hasFrameLabel_boolean = true;
          break;
        } 
      }
      
      return hasFrameLabel_boolean;
    }
    
    /**
     * Stop Timeline: Not sure why this is needed. A 'stop();' on frame 1 in the asset is there and should work
     * 
     * @param the_movieClip : *
     * @param isClickable_boolean Determines if the item will play a sound.
     * 
     * @return void 
     * 
     */		
    public static function doInitializeButtonMC(the_movieClip : *, isClickable_boolean : Boolean = true) : void 
    {
      if (the_movieClip) {
        if (isClickable_boolean) {
          //GO TO 'UP'
          the_movieClip.gotoAndStop("_up");
        } else {
          
          //GO TO 'DISABLED'
          if (DisplayObjectHelper.hasFrameLabel (the_movieClip, "_disabled") ) {
            the_movieClip.gotoAndStop("_disabled");
          } else {
            the_movieClip.gotoAndStop("_up");
            
          }
        }
        the_movieClip.buttonMode    = isClickable_boolean;
        the_movieClip.useHandCursor = isClickable_boolean;
        
        //TODO: maybe use clickability to add a sound that plays (and plays nice per howerver the SoundManager likes)
      }
    }
    
    /**
     * Remove all display list children from <code>Sprite</code>
     * 
     * @param parentToClear_sprite The <code>Sprite</code> to be cleared.
     * 
     * @return void
     * 
     */		
    public static function removeAllDisplayListChildrenFromParent(parentToClear_sprite : Sprite ) : void
    {
      if (parentToClear_sprite != null) {
        var numChildren_uint : uint = parentToClear_sprite.numChildren;
        while( numChildren_uint-- ) {
          parentToClear_sprite.removeChildAt(numChildren_uint);
        } 
      }
      
    } 
    
    /**
     * Remove all display list children from Sprite
     * 
     * @param child_displayobject <code>DisplayObject</code> To be removed
     * @param parent_displayobjectcontainer <code>DisplayObjectContainer</code> from which we want to remove the child
     * 
     * @return void
     * 
     */		
    public static function removeDisplayListChildFromParent(child_displayobject : DisplayObject, parent_displayobjectcontainer : DisplayObjectContainer ) : void
    {
      if (child_displayobject != null && parent_displayobjectcontainer != null && parent_displayobjectcontainer.contains(child_displayobject) ) {
        parent_displayobjectcontainer.removeChild(child_displayobject);
      }
      
    } 
    
    
    /**
     * Layout: Create a new TextField
     * 
     * @param x_uint <code>int</code>
     * @param y_uint <code>int</code>
     * @param width_uint <code>int</code>
     * @param height_uint <code>int</code>
     * @param textFormat <code>TextFormat</code>
     * 
     * @return TextField
     * 
     */
    public static function getNewTextField(x_int : int, y_int : int, width_int : int, height_int : int, textFormat : TextFormat = null) : TextField
    {
      
      var textField : TextField = new TextField();
      textField.x = x_int;
      textField.y = y_int; 
      textField.width = width_int;
      textField.height = height_int;
      textField.selectable = false;
      textField.border	 = false; //set to true to see placement better
      textField.multiline	 = true;
      
      
      //
      if (!textFormat) {
        var textFormat : TextFormat = textField.getTextFormat(); 
        textFormat.size	 = 30;
        textFormat.color = 0xFFFFFF; 
        textFormat.align = "right"; 
      }
      // 
      textField.defaultTextFormat =  ( textFormat); 
      
      return textField;
      
    }
    
    //PRIVATE	
    
  }
}